using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.Logic;
using NetProtocol.Client;
using NetProtocol.POD;
using System;
using System.Collections.Generic;
using NetProtocol.Enum;

namespace IQIGame.Onigao.GamePlay
{
    public class CBattleLogicModule : Singleton<CBattleLogicModule>, CNetBattleLogic
    {
        private CBattleLogicModule()
        {
        }

        public static CNetBattleLogic NetCore => Instance;

        private bool m_IsLocal = false;
        public bool IsLocal => m_IsLocal;

        ISession CNetBattleLogic.GetSession()
        {
            if (m_IsLocal)
            {
                return LocalLogicManager.Instance.localLogicClientSession;
            }
            return LogicServerModule.Instance.CurrSession;
        }

        public void InitLocalBattle(CreateBattlePOD battlePOD, LogicPlayerPOD playerPOD, Action<BattleEndData> onBattleEnd = null)
        {
            if (!battlePOD.isLocal)
            {
                throw new ArgumentException();
            }
            m_IsLocal = true;
            LocalLogicManager.Instance.Init(playerPOD);

            LogicPlayer logicPlayer = LocalLogicManager.Instance.logicPlayer;
            logicPlayer.playData.reason = ConnectLogicReason.TryEnterBattle;
            logicPlayer.playData.battleId = battlePOD.id;

            SBattleLogicModule.Instance.CreateBattle(battlePOD, onBattleEnd);
            SBattleLogicModule.Instance.OnPlayerConnect(logicPlayer, battlePOD.id);
        }

        // 表现层不需要关注逻辑层的生命周期
        public void ShutdownLocalBattle()
        {
            if (!m_IsLocal)
            {
                return;
            }
            m_IsLocal = false;

            Battle battle = SBattleLogicModule.Instance.GetLocalBattle();
            if (battle != null)
            {
                foreach (var player in new List<BattlePlayer>(battle.players))
                {
                    SBattleLogicModule.Instance.KickPlayerOut(player.id);
                }
                SBattleLogicModule.Instance.ShutdownBattle(battle.id);
            }
            LogicHelper.OnPlayerEnterBattle = null;
            LogicHelper.OnPlayerLeaveBattle = null;
        }

        void CNetBattleLogic.SC_NotifyEnd(ISession session, BattleEndPOD battleEndPod)
        {
            var rCmd = BattleManager.Instance.ctrl.queueCtrl.CreateEndCommandView(battleEndPod);
            BattleManager.Instance.ctrl.queueCtrl.EnQueue(rCmd);
        }

        void CNetBattleLogic.SC_NotifyLeave(ISession session)
        {
            session.Close(true); //提前主动关闭session, 防止出现网络断线的弹框
            BattleManager.Instance.ExitBattle();
        }

        void CNetBattleLogic.SC_NotifyEnter(ISession session, BattleEnterPOD battleEnter)
        {
            MsgDispatcher.Broadcast(MsgEventType.BattleView_Start, battleEnter);
            BattleManager.Instance.EnterBattle(battleEnter);
        }

        void CNetBattleLogic.SC_NotifyStart(ISession session)
        {
            BattleManager.Instance.BattleStartNow();
        }

        void CNetBattleLogic.SC_NotifyUpdates(ISession session, List<BattleUpdatePOD> updates)
        {
            this.PushShowQueue(updates);
        }

        private void PushShowQueue(List<BattleUpdatePOD> rPod)
        {
            BattleManager.Instance.ctrl.queueCtrl.EnQueue(rPod);
        }
    }
}